<template>
  <div class="df-search-title flex jcb mb15 unselect">
    <span class="title">{{title}}</span>
    <div class="search-time">
      <div class="btns dib vam mr20" v-if="hasDateType">
        <div class="btn dib vam csp"
             :class="{active: i === currType}"
             @click="currTypeChange(i, text)"
             v-for="(text, i) in options" :key="i">{{text.label || text}}
        </div>
      </div>
      <el-date-picker class="date-picker"
                      v-if="hasDate"
                      ref="datePicker"
                      @change="datePickerChange"
                      v-model="dateArr"
                      type="daterange"
                      :default-time="[,'23:59:59']"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
      </el-date-picker>
      <el-button v-if="hasSearch" type="primary" class="sure ml20" @click="search">查 询</el-button>
      <el-button v-if="hasReset" plant class="reset" @click="reset">重 置</el-button>
    </div>
  </div>
</template>

<script>
  export default {
    name: "DfSearchTitle",
    props: {
      options: {
        type: Array,
        default() {
          return [
            {label: '今日', value: 0},
            {label: '近7天', value: 7},
            {label: '本月', value: 'tm'},
            {label: '近1年', value: '1y'},
          ];
        },
      },
      title: {
        type: String,
        default: '',
      },
      hasDate: {
        type: Boolean,
        default: false,
      },
      hasReset: {
        type: Boolean,
        default: false,
      },
      hasSearch: {
        type: Boolean,
        default: false,
      },
      hasDateType: {
        type: Boolean,
        default: false,
      },
      defaultType: {
        default: '1y'
      },
      defaultIndex: {
        type: Number | String,
        default: '',
      },
    },
    data() {
      return {
        currType: 0,
        dateArr: [],
      };
    },
    created() {
      this.reset();
    },
    methods: {
      currTypeChange(val, item) {
        this.currType = val;
        this.typeToDate(item.value);
        this.$emit('btnChange', val, this.search, this.options);
        this.$emit('btn-change', val, this.search, this.options);
      },
      datePickerChange(val) {
        if (this.hasDateType && val) {
          for (let ind in this.options) {
            let item = this.options[+ind];
            let arr = [];
            item && item.value !== undefined && (arr = this.tools.typeToDate(item.value));
            if (+arr[0] === +val[0] && +arr[1] === +val[1]) {
              this.currType = +ind;
              return;
            }
          }
        }
        this.currType = '';
      },
      search() {
        this.$emit('search', this.dateArr);
      },
      reset() {
        let val = this.defaultType;
        if (this.defaultIndex === '') {
          this.currType = this.options.map(e => e.value).indexOf(this.defaultType);
          this.currType = Math.max(0, this.currType);
        } else {
          this.currType = this.defaultIndex * 1;
          val = this.options[this.currType].value;
        }
        this.hasDate && this.hasDateType && this.typeToDate(val || this.options[0].value);
        this.$emit('reset', this.dateArr);
        this.search();
      },
      typeToDate(val) {
        if (!this.hasDate) return [];
        this.dateArr = this.tools.typeToDate(val);
        return this.dateArr;
      },
    },
  }
</script>

<style scoped lang="less">
  .df-search-title {
    padding: 20px 40px;
    align-items: center;
    line-height: 1;
    font-size: 14px;
    color: #666;
    background-color: #fff;
    .title {
      color: #333;
      font-size: 16px;
      font-weight: 700;
      line-height: 40px;
    }
    .btn {
      padding: 4px 8px;
      border-radius: 4px;
      &.active {
        color: #fff;
        background-color: #1890ff;
      }
    }
  }
</style>